package com.sdtele.tta.operation;


import com.sdtele.tta.dac.Dac;
import com.sdtele.tta.models.JsonRequest;
import com.sdtele.tta.models.JsonResponse;
import com.sdtele.tta.models.User;
import com.sdtele.tta.models.VoteOperation;



/**
 * 操作类实现：GetUser(json.operation=getUser)
 * 
 * @author XCream
 */
public class OperationChgVoteReader extends Operation {

	@Override
	public JsonResponse handle(JsonRequest jsonRequest) {
		JsonResponse jres = new JsonResponse();
		long voteId = jsonRequest.getJsonObject().get("voteId").getAsLong();
		long action= jsonRequest.getJsonObject().get("action").getAsLong();
		long userId= jsonRequest.getJsonObject().get("userId").getAsLong();
		User curUser = (User) request.getSession().getAttribute("user");
		User voteOp=Dac.getInstance().getVoteOperationbyOwner(voteId);
		
		if (curUser == null) {
			jres.setStatus(1);
			jres.setMessage("用户未登录");
			return jres;
		}
          if(action==1) { 
        	  if(voteOp.getId()==curUser.getId()){
        
        	 VoteOperation voteOperation=new VoteOperation();
              	 voteOperation.setVoteId(voteId);
              	 voteOperation.setUserId(userId);
              	 voteOperation.setOperation(2);
              	  Dac.getInstance().addVoteOperation(voteOperation);
              	 User raUser=Dac.getInstance().getUser(voteOperation.getUserId());
              	  if(voteOperation.getId()>0) {
              		jres.setStatus(0);
      				jres.setMessage("添加可见人成功");
      				jres.setEntity(raUser);
        	  }else {
        			jres.setStatus(1);
      				jres.setMessage("添加可见人失败");
        	  }
       }else {
    	   jres.setStatus(1);
		  jres.setMessage("用户非投票任务创建者，添加可见人失败");
       }
    }
        	  if(action==0) {
            	if(voteOp.getId()==curUser.getId()){
            		  if( Dac.getInstance().delVoteoperation(voteId, userId, 2)) {
                    		jres.setStatus(0);
            				jres.setMessage("删除可见人成功");
                    	}else {
            			jres.setStatus(1);
          				jres.setMessage("删除可见人失败");
            	  }
           }else {
        	   jres.setStatus(1);
    		  jres.setMessage("用户非投票任务创建者，添加可见人失败");
           }
        }
		
        return  jres ;
}
	   
}